/*
 * @Author: your name
 * @Date: 2021-10-13 14:30:56
 * @LastEditTime: 2021-10-30 10:30:28
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /egg_dome/app/controller/home.js
 */
'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }
  async logins(){
    const {ctx, config} = this;
    const {code} = ctx.request.body;
    const result = await ctx.curl('https://gitee.com/oauth/token', {
      method: 'POST',
      data: {
        grant_type: 'authorization_code',
        client_id: config.userInfo.client_id,
        redirect_uri: config.userInfo.redirect_uri,
        client_secret: config.userInfo.client_secret,
        code
      },
      dataType: 'json'
    })

    // 通过接口来获取 token值
    const access_token = result.data.access_token;
    // 在通过接口token值来获取用户信息
    const newUser = await this.ctx.curl(`https://gitee.com/api/v5/user?access_token=${access_token}`, {
      method: 'GET',
      dataType: 'json'
    })
    ctx.body = newUser;
    
  }
}

module.exports = HomeController;
